package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.dao.local.ExamAnswerMapper;
import com.zhijian.medical.entity.po.ExamAnswer;
import com.zhijian.medical.entity.vo.request.ExamAnswerSearchVo;
import com.zhijian.medical.entity.vo.response.ExamAnswerVo;
import com.zhijian.medical.service.ExamAnswerService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 考核-录入答案表业务处理层
 *
 * @author HCG
 * @date 2022-01-04 15:53:36
 * @version: 1.0
 */
@Slf4j
@Service
public class ExamAnswerServiceImpl implements ExamAnswerService {

    @Autowired
    private ExamAnswerMapper mapper;

    @Override
    public ExamAnswer selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public ExamAnswerVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByWarehouseId(Long warehouseId) {
        mapper.deleteByWarehouseId(warehouseId);
    }

    @Override
    public Long insert(ExamAnswer examAnswer) {
        Date now = new Date();
        examAnswer.setCreateTime(now);
        examAnswer.setUpdateTime(now);
        return mapper.insert(examAnswer) == 1 ? examAnswer.getId() : 0L;
    }

    @Override
    public boolean update(ExamAnswer examAnswer) {
        Date now = new Date();
        examAnswer.setUpdateTime(now);
        return mapper.updateByPrimaryKey(examAnswer) != 0;
    }

    @Override
    public Integer count(ExamAnswerSearchVo examAnswerSearchVo) {
        return mapper.count(examAnswerSearchVo);
    }

    @Override
    public List<ExamAnswerVo> selectList(ExamAnswerSearchVo examAnswerSearchVo) {
        return mapper.selectList(examAnswerSearchVo);
    }

    @Override
    public ExamAnswerVo selectRightVoByQuestionId(Long questionId) {
        List<ExamAnswerVo> examAnswerVos = mapper.selectList(ExamAnswerSearchVo.builder()
                .questionId(questionId)
                .weatherRight((byte) 0)
                .build());
        return CollectionUtil.isNotEmpty(examAnswerVos) ? examAnswerVos.get(0) : null;
    }

    @Override
    public List<ExamAnswerVo> selectRightVoListByQuestionId(Long questionId) {
        return mapper.selectList(ExamAnswerSearchVo.builder()
                .questionId(questionId)
                .weatherRight((byte) 0)
                .build());
    }

    @Override
    public Page<ExamAnswerVo> selectPage(ExamAnswerSearchVo examAnswerSearchVo) {
        //先直接查总数
        int count = count(examAnswerSearchVo);
        //分页
        Page<ExamAnswerVo> page = new Page<>(true, count, examAnswerSearchVo.getPageNum(), examAnswerSearchVo.getPageSize());
        examAnswerSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<ExamAnswerVo> responseVos = this.selectList(examAnswerSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<ExamAnswer> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

}